package com.example.springboottools.sap;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

import redis.clients.jedis.*;

public class RedisCachePool {
    static Logger logger = LoggerFactory.getLogger(RedisCachePool.class);

    static PropertyUtil propertyUtil = new PropertyUtil("redis.properties");
    //static int maxpool = propertyUtil.getPropertyInt("redisMaxActive");
    static int jedispool = propertyUtil.getPropertyInt("JedisPool");
    // static LinkedList<JedisCluster> pool = new LinkedList<JedisCluster>();
    // static int count = 0;
    static Random random = null;

    static JedisCluster jc = null;

    static {
        try {
            Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
            jedisClusterNodes.add(new HostAndPort(propertyUtil.getPropertyStr("redis.ip1"),
                    propertyUtil.getPropertyInt("redis.port1")));
            jedisClusterNodes.add(new HostAndPort(propertyUtil.getPropertyStr("redis.ip2"),
                    propertyUtil.getPropertyInt("redis.port2")));
            jedisClusterNodes.add(new HostAndPort(propertyUtil.getPropertyStr("redis.ip3"),
                    propertyUtil.getPropertyInt("redis.port3")));
            jedisClusterNodes.add(new HostAndPort(propertyUtil.getPropertyStr("redis.ip4"),
                    propertyUtil.getPropertyInt("redis.port4")));
            jedisClusterNodes.add(new HostAndPort(propertyUtil.getPropertyStr("redis.ip5"),
                    propertyUtil.getPropertyInt("redis.port5")));
            jedisClusterNodes.add(new HostAndPort(propertyUtil.getPropertyStr("redis.ip6"),
                    propertyUtil.getPropertyInt("redis.port6")));

            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(jedispool);
            config.setMaxIdle(10);
            config.setMinIdle(50);
            config.setMaxWaitMillis(-1);
            //config.set
            config.setTestOnBorrow(true);//在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
            jc = new JedisCluster(jedisClusterNodes, 5 * 1000,50, config);
            // for (int i = 0; i < maxpool; i++) {
            // pool.add(new JedisCluster(jedisClusterNodes, 5 * 1000, config));
            // }
        } catch (Exception e) {
            logger.info("redis初始化失败");
            throw new ExceptionInInitializerError("redis初始化失败");
        }

    }

    public static JedisCluster getJedisCluster() {

        // if (pool.size()>0) {
        // if (count==maxpool) {
        // count=0;
        // }
        // int ra = (int) (Math.random() * 10);
        // JedisCluster jc = pool.get(ra);
        // count++;
        return jc;
        // }else {
        // log.info("服务器忙");
        // throw new RuntimeException("服务器忙");
        // }

    }

    public static void release(JedisCluster jc) {
        // pool.addLast(jc);
    }

    public static void main(String[] args) {
        String sss= "9BA35A64701102971D0D1D36AF16CC5A";
        String runvalue="9585214212";
        List<String> skuList = new ArrayList<>();
        Long runLong = Long.valueOf(runvalue);
        for (int i = 0; i < 100; i++) {
            skuList.add(String.valueOf(runLong++));
        }
        System.out.println(skuList);
        System.out.println(runLong);
        for (int i = 0; i < 100; i++) {
            skuList.add(String.valueOf(runLong++));
        }
        System.out.println(skuList);
        System.out.println(runLong);
    }
}
